package com.hutao.medical.common.util.disk;

import java.io.File;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hutao.medical.common.util.StringUtil;


public class DiskManageUtil {
	private static final Logger log = LoggerFactory.getLogger(DiskManageUtil.class);
	
	/**
	 * 某个文件是否在选好的磁盘目录里面 
	 * @param allDiskName 所有的磁盘目录
	 * @param currentFilePath 当前文件名称
	 * @return
	 */
	public static boolean isExistsFileInAllDisk(String allDiskName, String currentFilePath) {
		boolean flag = false;
		String[] diskArr = allDiskName.split(",");
		
		for (String diskName : diskArr) {
			File file = new File(diskName.trim() + currentFilePath.substring(1));
			
			if (file.exists()) {
				flag = true;
				log.info("---currentFilePath文件已经存在---" + currentFilePath);
				break;
			}
		}
		
		return flag;
	}
	
	public static String getDiskNameByFileName(String allDiskName, String currentFilePath) {
		String findDiskName = "";
		String[] diskArr = allDiskName.split(",");
		
		for (String diskName : diskArr) {
			File file = new File(diskName.trim() + currentFilePath.substring(1));
			
			if (file.exists()) {
				findDiskName = diskName.trim();
				break;
			}
		}
		
		return findDiskName;
	}
	
	public static String getFileRealPath(String allDiskName, String currentFilePath) {
		String filePath = "";
		String realDiskName = getDiskNameByFileName(allDiskName, currentFilePath);
		
		if (StringUtils.isNotBlank(realDiskName)) {
			filePath = realDiskName + currentFilePath.trim().substring(1);
		}
		
		return filePath;
	}
	
	public static void fileHashStorageRootUrl(List<Map<String, Object>> scanFileList, String configFileStorageAllDisk, String uploadResourcePath,
			String hospitalizationNoteResourceUrl, String firstGuid) {
		if (CollectionUtils.isNotEmpty(scanFileList)) {
			for (Map<String, Object> scanFileMap : scanFileList) {
				String fileHash = MapUtils.getString(scanFileMap, "fileHash");
				String fileFourDisk = MapUtils.getString(scanFileMap, "fileUrl");
				String fileName = MapUtils.getString(scanFileMap, "fileName");
				
				//当前存储本地文件路径
				String filePath = uploadResourcePath + fileFourDisk + fileName;
				
				String diskName = DiskManageUtil.getDiskNameByFileName(configFileStorageAllDisk, 
						uploadResourcePath + fileFourDisk + fileName);
				
				if (StringUtil.isBlank(diskName)) {
					log.error("fileHash在本地磁盘文件上找不到, fileHash={}, firstGuid={}", fileHash, firstGuid);
				}
				
				scanFileMap.put("hospitalizationNoteResourceUrl", hospitalizationNoteResourceUrl.replace("upload", "upload" + diskName.trim()));
			}
		}
		
	}
	
	
	
	
	
	
	
	
	
	

}





